package com.piece.module.spider.concurrent;

import com.piece.core.mongo.entity.MongoJobLog;
import com.piece.module.spider.context.SpiderContext;
import com.piece.module.spider.context.SpiderContextHolder;
import com.piece.module.spider.context.SpiderJobContext;
import com.piece.module.spider.executor.Spider;
import com.piece.module.spider.model.SpiderFlow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * 爬虫定时执行
 */
@Slf4j
@Component
@RefreshScope
public class SpiderJob {

	@Autowired
	private Spider spider;

	private static Map<String, SpiderContext> contextMap = new HashMap<>();

	@Value("${spider.job.enable:true}")
	public boolean spiderJobEnable;

	@Value("${spider.workspace:spider}")
	public String workspace;

	public void run(SpiderFlow spiderFlow, MongoJobLog jobLog) throws Exception {
		SpiderJobContext context = null;
		try {
			context = SpiderJobContext.create(this.workspace, spiderFlow.getId().toString(), jobLog.getLogId(),false);
			SpiderContextHolder.set(context);
			contextMap.put(jobLog.getLogId(), context);
			spider.run(spiderFlow, context);
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			if (null != context) {
				context.close();
			}
			contextMap.remove(jobLog.getLogId());
			SpiderContextHolder.remove();
		}
	}

	public static SpiderContext getSpiderContext(String id) {
		return contextMap.get(id);
	}
}
